import { Toast } from "vant";
import {
  updateFriendDynamicReadStatus,
  getFriendDynamic,
  getFriendDynamicById,
} from "@/request/friend-dynamic-request";

export const useUpdateFriendDynamicReadStatus = async (store, _id) => {
  try {
    const result = await updateFriendDynamicReadStatus(_id);
    if (result.code === 201 && result.message === "success") {
      store.dispatch("dynamic/setNewDynamicCount", 0);
    }
  } catch (error) {
    Toast(error.message);
  }
};

export const useGetFriendDynamic = async (finished, loading, list, _id) => {
  try {
    const result = await getFriendDynamic(_id);
    if (result.code === 201 && result.message === "success") {
      console.log(result);
      list.value = result.data[0]?.dynamics.reverse();
      finished.value = true;
      loading.value = false;
    } else {
      Toast(result.message);
    }
  } catch (error) {
    Toast(error.message);
  }
};

export const useGetFriendDynamicByID = async (finished, loading, list, _id) => {
  try {
    const result = await getFriendDynamicById(_id);
    if (result.code === 201 && result.message === "success") {
      list.value = result.data.reverse();
      finished.value = true;
      loading.value = false;
    } else {
      Toast(result.message);
    }
  } catch (error) {
    Toast(error.message);
  }
};
